//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;

namespace OpenLS.Chart.UI
{
    public class DataPointsObserver: INotifyPropertyChanged
    {
        public DataPointsObserver(DataPoints points)
        {
            dataPoints = points;
            dataPoints.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(dataPoints_CollectionChanged);
        }

        void dataPoints_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            Generation++;
        }

        private int generation;
        public int Generation
        {
            get { return generation; }
            private set { generation = value;
                notifyPropertyChanged("Generation"); }
        }

        private void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        readonly DataPoints dataPoints;
        [Obsolete]
        internal DataPoint GetDataPoint(int index)
        {
            if (dataPoints.Contains(index))
                return dataPoints[index];
            return null;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
